#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <pthread.h>
#include <string>
#include <cstdint>
#include <string.h>
#include <functional>
#include "Log.hpp"

namespace ThreadModlue{
    static uint32_t number = 0;
    using namespace LogModule;
    //template<class T> 
    class Thread
    {
        using func_t = std::function<void()>;
    private:
        void EnableDetach()
        {
            _isdetach = true;
        }

        void EnableRunning()
        {
            _isrunning = true;
        }
        
    public:
        Thread(func_t func)
        : _tid(0),
         _isdetach(false), 
         _isrunning(false), 
         _res(nullptr),
         _func(func)
        {
            _name = "thread_" + std::to_string(number++);
            
        }

        pthread_t Tid() { return _tid; }
        std::string Name() { return _name; }
        bool Isdetach() { return _isdetach; }
        bool Isrunning() { return _isrunning; }
        void* Res() { return _res; }

        static void *Routine(void *args) //重点
        {
            Thread *self = static_cast<Thread*>(args);
            pthread_setname_np(self->_tid, self->_name.c_str());
            self->EnableRunning();
            if(self->_isdetach)
            {
                pthread_detach(self->_tid); 
            }
            self->_func();
            return 0;
        }

        void Detach()
        {
            if(_isdetach) return;
            if(_isrunning) pthread_detach(_tid);
            EnableDetach();
            //std::cout << _name << "被分离" << std::endl;
        }

        bool Start()
        {
            if(_isrunning) return false;
            int n = pthread_create(&_tid, nullptr, Routine, this); // 传入this指针
            if(n != 0)
            {
                //std::cerr << "create thread error:" << strerror(n) << std::endl;
                return false;
            }
            else
            {
                //std::cout << "create thread success" << std::endl;
            }
            //std::cout << _name << "开始运行" << std::endl;
            return true;
        }

        bool Stop()
        {
            if(_isrunning)
            {
                int n = pthread_cancel(_tid);
                if(n != 0)
                {
                    std::cerr << "cancel thread error:" << strerror(n) << std::endl;
                    return false;
                }
                _isrunning = false;
                //std::cout << _name << "停止运行" << std::endl;
                return true;
            }
            return false;
        }

        bool Join()
        {
            if(_isdetach)
            {
                //std::cout << _name << "被分离,无法join" << std::endl;
                return false;
            } 
            int n = pthread_join(_tid, &_res);
            if(n != 0)
            {
                //std::cerr << "join thread error:" << strerror(n) << std::endl;
                LOG(LogLevel::DEBUG) << _name << "join失败"; 
                return false;
            }
            //std::cout << _name << "回收成功" << std::endl;
            LOG(LogLevel::DEBUG) << _name << "join成功"; 
            return true;
        }

        ~Thread() {}
    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach; 
        bool _isrunning;
        void* _res; 
        func_t _func;
        //T _data;
    };
}


#endif